var App = App||{};
App.Clipboard = {};

App.Clipboard.copy = function(el){ if (el.is('.row')) this.copyRow(el); else if (el.is('.day')) this.copyDay(el); };
App.Clipboard.paste = function(el){ if (el.is('.row')) this.pasteRow(el); else if (el.is('.day')) this.pasteDay(el); };

App.Clipboard.clear = function(){ delete this.clip; };
App.Clipboard.add = function(el){ this.clip = this.clip||[]; this.clip.push(el); };
App.Clipboard.get = function(){ if (this.clip && this.clip.length) return this.clip[this.clip.length-1]; else return false; };



App.Clipboard.getDataFromRow = function(row){
	var projEl = row.find('.project'), description = row.find('.description'), rowData = { projectId: projEl.data('projectId'), projectName: projEl.text() };
	if (description.find('textarea').length) rowData.description = description.find('textarea').val();		// if edit mode
	else rowData.description = description.text();
	return rowData;
};

App.Clipboard.copyRow = function(row){ this.add(this.getDataFromRow(row)); };

App.Clipboard.pasteRow = function(row, rowData){
	var clip = rowData || this.get();
	if (!clip) return;
	if (clip.projectId) row.find('.project').data('projectId', clip.projectId).html('<div class="remove-project"></div>'+clip.projectName).removeClass('empty project_0');	
	if (clip.description || clip.projectId) row.find('.description textarea').val(clip.description);
};



App.Clipboard.copyDay = function(day){
	var rows, isEditable = day.find('.editable-clone').length, i = 0, rowData = [], rowsL;
	if (isEditable) rows = day.find('.editable-clone .row');
	else rows = day.find('.content-readonly .row');
	rowsL = rows.length;
	for (; i < rowsL; i++) rowData.push(this.getDataFromRow($(rows[i])));
	this.add(rowData);
};

App.Clipboard.pasteDay = function(day){
	var clip = this.get();
	if (!clip) return;
	var i = 0, rows = day.find('.editable-clone .row'), rowsL = rows.length;
	if ($.isArray(clip)) for (; i < rowsL; i++) this.pasteRow($(rows[i]), clip[i]);			// clip is a day
	else for (; i < rowsL; i++) this.pasteRow($(rows[i]), clip);							// clip is a row
};
